<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Hospital;
use App\Models\Area;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class HospitalController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Hospital(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
//            $grid->column('province');
            $grid->column('city');

            $grid->column('county');
            $grid->column('is_appoint')->switch('', true);

//            $grid->column('longitude');
//            $grid->column('latitude');
//            $grid->column('intro');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Hospital(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('province');
            $show->field('county');
            $show->field('city');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('intro');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Hospital(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->select('province')->options(Area::query()->where('parent_id',0)->pluck('title','id'))->load('city', '/api/city');
            $form->select('city')->load('county', '/api/city');
            $form->select('county');
            $form->text('longitude');
            $form->text('latitude');
            $form->text('intro')->default("为了宝宝享受更好的健康服务我们一起加油");
            $form->text('code','中心编码');
            $form->text('precinct','所属派出所');
            $form->text('post_code','邮编');
            $form->text('street','街道');

            $form->switch('is_family','是否开通家医')
                ->saving(function ($v) {
                    return $v ? 1 : 0;
                });
            $form->switch('independent','是否为独立医院')
                ->saving(function ($v) {
                    return $v ? 1 : 0;
                });
            $form->switch('is_appoint','是否开通预约')
                ->saving(function ($v) {
                    return $v ? 1 : 0;
                });
//            $form->hasMany('links', function (Form\NestedForm $form) {
//                $form->text('name')->label('名称');
//                $form->url('link')->label('链接');
//                //$form->hidden('hospital_id')->value(4);
//            })->label('首页快捷链接')->useTable()->saving(function ($value) {
//                return json_encode($value);
//            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
